package com.starsoft.smdc.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.starsoft.frame.core.orm.BaseDao;
import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.smdc.bean.SmdcMarketDaily;
import com.starsoft.smdc.bean.SmdcMarketDailyId;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.util.SecurityType;

@Repository
public class MarketDailyDao extends BaseDao<SmdcMarketDaily, SmdcMarketDailyId>{

	public Date getMaxDate(SmdcSecurity security){
		String hql = "select max(id.tradeDate) from SmdcMarketDaily where id.secId=?0";
		return (Date) getValue(hql, security.getSecId());
	}
	
	public Date getNavMaxDate(SmdcSecurity security){
		String hql = "select max(id.tradeDate) from SmdcMarketDaily where id.secId=?0";
		if(SecurityType.isFund(security.getSecType())){
			hql = "select max(id.tradeDate) from SmdcMarketDaily where id.secId=?0 and secNav is not null";
		}
		return (Date) getValue(hql, security.getSecId());
	}
	
	public Date getHandicapMaxDate(SmdcSecurity security){
		String hql = "select max(id.tradeDate) from SmdcMarketDaily where id.secId=?0  and buy1 is not null";
		return (Date) getValue(hql, security.getSecId());
	}
	
	public Date getValuationMaxDate(SmdcSecurity security){
		String 	hql = "select max(id.tradeDate) from SmdcMarketDaily where id.secId=?0 and peWeighted is not null";
		return (Date) getValue(hql, security.getSecId());
	}
	
	public Date getFullEarningMaxDate(SmdcSecurity security) {
		String hql = "select max(id.tradeDate) from SmdcMarketDaily where id.secId=?0 and divi is not null";
		if (StringUtil.isEmpty(security.getFullEarningIndex())) {
			hql = "select max(id.tradeDate) from SmdcMarketDaily where id.secId=?0";
		}
		return (Date) getValue(hql, security.getSecId());
	}
	
	public List<SmdcMarketDaily> getAfter(String secId, Date beginDate){
		String hql ="from SmdcMarketDaily where id.secId=?0 and id.tradeDate>=?1 order by id.tradeDate asc";
		return find(hql, secId, beginDate);
	}
	
	public List<SmdcMarketDaily> getDateAll(Date tradeDate){
		String hql ="from SmdcMarketDaily where id.tradeDate=?0";
		return find(hql, tradeDate);
	}
	
	public List<SmdcMarketDaily> getBetweenAnd(String secId, Date beginDate, Date endDate){
		String hql ="from SmdcMarketDaily where id.secId=?0 and id.tradeDate>=?1 and id.tradeDate<=?2 order by id.tradeDate asc";
		return find(hql, secId, beginDate, endDate);
	}
	
	@SuppressWarnings("unchecked")
	public List<SmdcMarketDaily> getSomeBefore(String secId, int count, Date endDate){
		String hql ="from SmdcMarketDaily where id.secId=?0 and id.tradeDate<?1 order by id.tradeDate desc";
		Query queryObject = getQuery(hql, secId, endDate);
		queryObject.setMaxResults(count);
		return queryObject.list();
	}

	public Map<String, Object> pageQuery(PageObject po, String secId, Date beginDate, Date endDate) {
		List<Criterion> criterions = new ArrayList<Criterion>();
		String sort = po.getSort().replace("secId", "id.secId").replace("tradeDate", "id.tradeDate");
		po.setSort(sort);
		if(!StringUtil.isEmpty(secId)){
			criterions.add(Restrictions.like("id.secId", secId, MatchMode.ANYWHERE));
		}
		if(beginDate!=null){
			criterions.add(Restrictions.ge("id.tradeDate", beginDate));
		}
		if(endDate!=null){
			criterions.add(Restrictions.le("id.tradeDate", endDate));
		}
		return pageQuery(entityClass, po, criterions);
	}

	public int deleteBySecId(String secId){
		String hql ="delete from SmdcMarketDaily where id.secId=?0";
		return update(hql, secId);
	}
	
}
